package com.liuya.base;

import java.util.List;
import java.util.Map;

import com.liuya.common.InstanceUtil;
import com.liuya.page.Pageable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

/**
 * 业务逻辑层基类
 * 继承基类后必须配置CacheConfig(cacheNames="")
 *
 * @author liuya
 * @date 20160824
 */
public abstract class AbstractBaseProvider<T extends BaseModel> extends BaseMethods<T> implements BaseProvider<T> {
    protected Logger logger = LogManager.getLogger();
    protected AbstractBaseProvider<T> getProvider() {
        return InstanceUtil.getBean(getClass());
    }

    /**
     * 根据Id查询(默认类型T)
     */
    public List getList(List<String> ids) {
        List list = InstanceUtil.newArrayList();
        if (ids != null) {
            for (String id : ids) {
                list.add(getProvider().selectById(id));
            }
        }
        return list;
    }

    /**
     * 根据Id查询(cls返回类型Class)
     */
    public <K> List<K> selectList(List<String> ids, Class<K> cls) {
        List<K> list = InstanceUtil.newArrayList();
        if (ids != null) {
            for (String id : ids) {
                BaseModel t = getProvider().selectById(id);
                K k = InstanceUtil.to(t, cls);
                list.add(k);
            }
        }
        return list;
    }

    @Transactional
    public T selectById(String id) {
        return super.selectById(id);
    }

    public Pageable<T> selectPage(Map<String, Object> params) {
        this.startPage(params);
        Pageable<T> pageInfo = new Pageable<T>(mapper.selectAll(params));
        return pageInfo;
    }

//    /**
//     * 根据Id查询(默认类型T)
//     */
//    public Pageable<T> selectPageByIds(Page<String> ids) {
//        if (ids != null) {
//            Page<T> page = new Page<T>(ids.getPageNum(), ids.getPageSize());
//            page.setTotal(ids.getTotal());
//            page.clear();
//            AbstractBaseProvider<T> provider = getProvider();
//            for (String id : ids) {
//                page.add(provider.selectById(id));
//            }
//            return new Pageable<T>(page);
//        }
//        return new Pageable<T>();
//    }

//    /**
//     * 根据Id查询(cls返回类型Class)
//     */
//    public <K> PageInfo<K> selectPageByIds(Page<String> ids, Class<K> cls) {
//        if (ids != null) {
//            Page<K> page = new Page<K>(ids.getPageNum(), ids.getPageSize());
//            page.setTotal(ids.getTotal());
//            page.clear();
//            AbstractBaseProvider provider = getProvider();
//            for (String id : ids) {
//                BaseModel t = provider.selectById(id);
//                K k = InstanceUtil.to(t, cls);
//                page.add(k);
//            }
//            return new PageInfo<K>(page);
//        }
//        return new PageInfo<K>();
//    }

    public void add(T record) {
        update(record);
    }

    @Transactional
    public void delete(String id, String userId) {
        deleteById(id, userId);
    }

    @Transactional
    public T update(T record) {
        return (T) super.update(record);
    }

}
